<?php

namespace HelloAdmin\Controller;

// 后台用户模块
class TeacherController extends CommonController {

    private $Teacher;

    public function __construct() {
        parent::__construct();
        $this->Teacher = D("Teacher");
    }

    public function index() {
        $where = '1 = 1';
        $post = $this->GetParam('name,mobile');
        if (!empty($post['name'])) {
            $where .= ' AND `name` like ' . "'{$post['name']}%'";
        }
        if (!empty($post['mobile'])) {
            $where .= ' AND `mobile` = ' . "'{$post['mobile']}'";
        }

        $count = $this->Teacher->where($where)->count($this->Teacher->pk);
        parent::backPage($count);
        $data = $this->Teacher->where($where)->limit($this->offset, $this->numPerPage)->select();
        $this->assign('data', $data);

        $this->display();



        $this->display();
    }

    public function add() {


        $this->display();
    }
    
    public function insert(){
        $post = $this->GetParam('family_name,last_name,mobile,age,sex,teach_type,dring,Cart_cart_id,orgLookup_ground_id,is_recommend,disabled,avatar,mem_type');
        
        $data['family_name'] = $post['family_name'];
        $data['last_name'] = $post['last_name'];
        $data['name'] = $post['family_name'].$post['last_name'];
        $data['mobile'] = $post['mobile'];
        $data['age'] = $post['age'];
        $data['sex'] = $post['sex'];
        $data['teach_type'] = $post['teach_type'];
        $data['dring'] = $post['dring'];
        $data['cart_id'] = $post['Cart_cart_id'];
        $data['ground_id'] = $post['orgLookup_ground_id'];
        $data['is_recommend'] = $post['is_recommend'];
        $data['disabled'] = $post['disabled'];
        $data['avatar'] = $post['avatar'];
        $data['create_time'] = time();
        $data['mem_type'] = $post['mem_type'];
        
        if($this->Teacher->SaveData($data)){
            $this->success('添加成功');
        }
        $this->error('添加失败');
        
    }

    public function foreverdelete() {
        if ($_GET['user_id'] == 1) {
            $this->error('操作被禁用！');
        }
        if ($_GET['user_id'] > 0) {
            if ($this->Teacher->where("`user_id` = '{$_GET['user_id']}'")->delete()) {
                $this->success('删除成功');
            }
        }
        //var_dump($this->Teacher->getLastSql());exit;
        $this->error('删除失败');
    }

    public function coverImg() {
        $config = array(
            'maxSize' => 20000000, //上传的文件大小限制 (0-不做限制)
            'savePath' => '/Teacher/', //保存路径
            'autoSub' => true,
        );
        $res = $this->UploadImg($config);
        exit(json_encode($res));
    }

}
